package com.lt.lumentracks.controller;


import com.lt.lumentracks.entity.po.ChatMessage;
import com.lt.lumentracks.entity.vo.common.PageVO;
import com.lt.lumentracks.entity.vo.common.Result;
import com.lt.lumentracks.service.IChatMessageService;
import com.lt.lumentracks.service.common.ConversationMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 聊天消息表 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-10-06
 */
@RestController
@RequestMapping("/chat-message")
public class ChatMessageController {

    @Autowired
    private IChatMessageService iChatMessageService;
    @Autowired
    private ConversationMessageService conv_msgService;

    // 增（新增消息），返回新增的消息id
    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated ChatMessage chatMessage){
        return conv_msgService.addMsg(chatMessage);
    }


    // 改（撤回消息）
    @PutMapping("/revokeMsg")
    public Result<String> revokeMsg(@RequestParam("mid") Long mid){
        return iChatMessageService.revokeMsg(mid);
    }


    // 查（根据会话id查询聊天记录）
    @GetMapping("/list")
    public Result<PageVO<ChatMessage>> list(@RequestParam("conversationId") Long conversationId,
                                            @RequestParam(defaultValue = "1") long pageNum,
                                            @RequestParam(defaultValue = "50") long pageSize){
        PageVO<ChatMessage> vo = iChatMessageService.getByPage(conversationId, pageNum, pageSize);
        return Result.success(vo);
    }

    // 查--根据对话双方id查询聊天记录
    @GetMapping("/getWhisperMsg")
    public Result<PageVO<ChatMessage>> getWhisperMsg(@RequestParam("talker_uid") Integer talker_uid,
                                                     @RequestParam(defaultValue = "1") long pageNum,
                                                     @RequestParam(defaultValue = "50") long pageSize){
        PageVO<ChatMessage> vo = conv_msgService.getWhisperMsg(talker_uid, pageNum, pageSize);
        return Result.success(vo);
    }


}
